package com.proton.carepatchtemp.utils.net;

import com.android.volley.Response;
import com.apkfuns.logutils.LogUtils;
import com.google.gson.reflect.TypeToken;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.daomanager.HealthTipManager;
import com.proton.carepatchtemp.database.daomanager.MsgManager;
import com.proton.carepatchtemp.database.table.HealthTip;
import com.proton.carepatchtemp.database.table.Msg;
import com.proton.carepatchtemp.utils.JSONUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.pair.ParamPair;
import com.proton.carepatchtemp.utils.net.pair.ResultPair;
import com.proton.carepatchtemp.utils.net.request.JGet;
import com.proton.carepatchtemp.utils.net.request.JPost;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import static com.proton.carepatchtemp.utils.net.ReportCenter.PS_RPT;


/**
 * Created by Api on 2016/11/3.
 */

public class MsgCenter extends DataCenter {
    //  每一页report条数
    public static final int PS_MSG = 10;  //每次请求的数据条数
    public static final String msggetlist = "/message/getlist";
    public static final String delete = "/message/delete";
    public static final String deleteSome = "/messages/delete";
    public static final String healthTips = "/healthtip/getHealthtipTitle";
    public static final String maxTempPush = "/healthtip/maxTempPush";

    public static boolean maxTempPush(Long profileId, Float maxTemps) {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList();
        params.add(new ParamPair(Extras.PRF_PROFILEID, profileId));
        params.add(new ParamPair(Extras.MSG_MAX_TEMP_PUSH, maxTemps));

        VolleyUtil.addRequest(new JPost(getApi(maxTempPush), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                if (response.isSuccess()) {

                } else {
//                    maxTempPush(profileId,maxTemps);
                }
                LogUtils.d(params);
                LogUtils.d(response);
            }
        }));

        return false;
    }

    /**
     * @param endTime 时间点，传入0表示获取最新的Size条数据
     * @param page    第几页
     * @param poll    0 = 获取endTime以前的最新pageSize条数据 下拉
     *                1 = 获取endTime以后的最新pageSize条数据 上拉
     */
    public static boolean getHealthTips(Long endTime, int page, int poll) {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList();
        params.add(new ParamPair(Extras.ENDTIME, endTime));
        params.add(new ParamPair(Extras.PG, page));
        params.add(new ParamPair(Extras.PS, PS_RPT));
        params.add(new ParamPair(Extras.RPT_POLL, poll));
        VolleyUtil.addRequest(new JPost(getApi(healthTips), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                if (response.isSuccess()) {
                    Type listType = new TypeToken<ArrayList<HealthTip>>() {
                    }.getType();
                    List<HealthTip> msgList = JSONUtils.getObj(msg, listType, "yyyy-MM-dd");

                    if (1 == poll) {
                        if (msgList.size() > 0) {
                            HealthTipManager.insertOrReplaceInTx(msgList);
                        }
                        EventBus.getDefault().post(new MainEvent(Codes.NET_HEALTH_TIPS_GETDOWNLIST, ret, ""));
                    } else if (0 == poll) {
                        HealthTipManager.deleteAll();
                        HealthTipManager.insertOrReplaceInTx(msgList);
                        EventBus.getDefault().post(new MainEvent(Codes.NET_HEALTH_TIPS_GETUPLIST, ret, ""));
                    }

                }
                LogUtils.d(params);
                LogUtils.d(response);
            }
        }));

        return false;
    }


    public static boolean msggetlist(int page, int type) {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList();
        params.add(new ParamPair(Extras.TYPE, type));
        params.add(new ParamPair(Extras.PG, page));
        params.add(new ParamPair(Extras.PS, 50));
        VolleyUtil.addRequest(new JGet(getApi(msggetlist), params, new Response.Listener<ResultPair>() {

            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int codeId = Codes.NET_MSG_GETLIST;
                if (response.isSuccess()) {
                    try {
                        Type listType = new TypeToken<ArrayList<Msg>>() {
                        }.getType();
                        if (null != jsonObject.get("content")) {
                            List<Msg> msgList = JSONUtils.getObj(jsonObject.get("content").toString(), listType, "yyyy-MM-dd");
                            if (null != msgList || msgList.size() > 0) {
                                //LogUtils.e(msgList.size());
                                MsgManager.deleteAll();
                                MsgManager.insertOrReplaceInTx(msgList);
                            }
                        }

                    } catch (Exception e) {
                        LogUtils.e(e);
                    }
                }
                EventBus.getDefault().post(new MainEvent(codeId, ret, ""));
            }
        }));
        return false;
    }

    public static boolean msgdelete(long id) {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList<>();
        params.add(new ParamPair(Extras.MESSAGEID, id));
        VolleyUtil.addRequest(new JPost(getApi(delete), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                int codeId = Codes.NET_MSG_DELETE;
                if (response.isSuccess()) {
                    try {
                        MsgManager.deleteMsgById(id);
                        LogUtils.e(ret);
                        LogUtils.e(msg);
                    } catch (Exception e) {
                        LogUtils.e(e);
                    }
                }
                EventBus.getDefault().post(new MainEvent(codeId, ret, msg));
            }
        }));
        return false;
    }

    public static boolean deleteSome(List<Long> ids) {
        if (DataCenter.noNet()) {
            return false;
        }
        List params = new ArrayList<>();
        params.add(new ParamPair(Extras.MSG_IDS, ids.toString().replace("[", "").replace("]", "").replaceAll(" ", "")));
        VolleyUtil.addRequest(new JPost(getApi(deleteSome), params, new Response.Listener<ResultPair>() {
            @Override
            public void onResponse(ResultPair response) {
                String ret = response.getName();
                String msg = response.getValue();
                int codeId = Codes.NET_MSG_DELETE;
                if (response.isSuccess()) {
                    try {
                        MsgManager.deleteSomeById(ids);
                        LogUtils.e(ret);
                    } catch (Exception e) {
                        LogUtils.e(e);
                    }
                }
                EventBus.getDefault().post(new MainEvent(codeId, ret, msg));
            }
        }));
        return false;
    }

}
